package edu.udel.libra.model.repository;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Clob;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
import java.util.logging.Logger;

public abstract class JdbcRepository
{
	private static final Logger LOG = Logger.getLogger(JdbcRepository.class.getName());

    protected Object[] getArgs(Object... args)
    {
        return args;
    }
    
    protected int[] getArgTypes(Object... args)
    {
        if (args == null)
            return null;
        
        int[] types = new int[args.length];
        
        for (int i = 0; i < types.length; ++i)
        {
            if (args[i] == null)
                types[i] = Types.NULL;
            else if (args[i].getClass().isAssignableFrom(String.class))
                types[i] = Types.VARCHAR;
            else if (args[i].getClass().isAssignableFrom(CharSequence.class))
                types[i] = Types.VARCHAR;
            else if (args[i].getClass().isAssignableFrom(Integer.class))
                types[i] = Types.INTEGER;
            else if (args[i].getClass().isAssignableFrom(BigDecimal.class))
                types[i] = Types.NUMERIC;
            else if (args[i].getClass().isAssignableFrom(Timestamp.class))
                types[i] = Types.TIMESTAMP;
            else if (args[i].getClass().isAssignableFrom(Time.class))
                types[i] = Types.TIME;
            else if (args[i].getClass().isAssignableFrom(Date.class))
                types[i] = Types.DATE;
            else if (args[i].getClass().isAssignableFrom(Long.class))
                types[i] = Types.BIGINT;
            else if (args[i].getClass().isAssignableFrom(BigInteger.class))
                types[i] = Types.BIGINT;
            else if (args[i].getClass().isAssignableFrom(Boolean.class))
                types[i] = Types.BOOLEAN;
            else if (args[i].getClass().isAssignableFrom(Clob.class))
                types[i] = Types.CLOB;
            
            LOG.info("Setting arg " + i + " (value=" + args[i] + ") to type " + types[i]);
        }
        
        return types;
    }
    
    protected String getValue(String val)
    {
        if (val == null)
            return "";
        return val;
    }
}
